<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="下单时间">
        <el-date-picker
          v-model="daterangeOrdertime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="用户姓名" prop="cusname">
        <el-input
          v-model="queryParams.cusname"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cusidcard">
        <el-input
          v-model="queryParams.cusidcard"
          placeholder="请输入客户身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="销售" prop="sales">
        <el-select v-model="queryParams.sales" placeholder="销售名字" clearable>
          <el-option
            v-for="dict in dict.type.svc_xs_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-select v-model="queryParams.channel" placeholder="渠道名字" clearable>
          <el-option
            v-for="dict in dict.type.svc_qd_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hpvorder:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:hpvorder:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdateAll"
          v-hasPermi="['system:hpvorder:updateall']"
        >批量修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hpvorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column label="ID" width="40" align="center" prop="id" />
      <el-table-column label="订单时间"  align="center" prop="ordertime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ordertime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="价格(元)" align="center" prop="price" />-->
      <el-table-column label="项目类别" width="80" align="center" prop="protype" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_pro_type" :value="scope.row.protype"/>
        </template>
      </el-table-column>
      <el-table-column label="姓名"width="80" align="center" prop="cusname" />
      <el-table-column width="180" label="身份证" align="center" prop="cusidcard" />
      <el-table-column width="120"label="手机号" align="center" prop="phonenumber" />
<!--      <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="婚否" align="center" prop="marry" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_user_marry" :value="scope.row.marry"/>
        </template>
      </el-table-column>-->
      <el-table-column label="订单状态"width="80" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="接种机构" align="center" prop="org" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_jg_name" :value="scope.row.org"/>
        </template>
      </el-table-column>
      <el-table-column label="首针" align="center" prop="one" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.one, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="二针" align="center" prop="two" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.two, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="三针" align="center" prop="three" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.three, '{y}-{m}-{d}') }}</span>
        </template>w
      </el-table-column>-->
      <el-table-column label="销售" width="100"align="center" prop="sales" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_xs_name" :value="scope.row.sales"/>
        </template>
      </el-table-column>

      <el-table-column label="渠道" width="100"align="center" prop="channel" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_qd_name" :value="scope.row.channel"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="300"align="center" prop="remark" />
      <el-table-column label="操作" width="80"align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:hpvorder:edit']"
          >修改</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:hpvorder:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改HPV接种信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col>
            <el-form-item label="项目类别" prop="protype">
              <el-select v-model="form.protype" placeholder="请选择项目类别">
                <el-option
                  v-for="dict in dict.type.sys_pro_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="cusname">
              <el-input v-model="form.cusname" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="订单时间" prop="ordertime">
              <el-date-picker clearable
                              v-model="form.ordertime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择订单创建时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格（元）" prop="price">
              <el-input v-model="form.price" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="cusidcard">
              <el-input v-model="form.cusidcard" placeholder="请输入用户身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接种机构" prop="org">
              <el-select v-model="form.org" placeholder="请选择接种机构">
                <el-option
                  v-for="dict in dict.type.svc_jg_name"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-radio-group v-model="form.sex">
                <el-radio
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否已婚">
              <el-radio-group v-model="form.marry">
                <el-radio
                  v-for="dict in dict.type.svc_user_marry"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>


            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首针时间" prop="one">
              <el-date-picker clearable
                              v-model="form.one"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择首针时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二针时间" prop="two">
              <el-date-picker clearable
                              v-model="form.two"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择二针时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="三针时间" prop="three">
              <el-date-picker clearable
                              v-model="form.three"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择三针时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售" prop="sales">
              <el-select v-model="form.sales" placeholder="请选择销售">
                <el-option
                  v-for="dict in dict.type.svc_xs_name"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="12">
            <el-form-item label="渠道" prop="channel">
              <el-select v-model="form.channel" placeholder="请选择渠道">
                <el-option
                  v-for="dict in dict.type.svc_qd_name"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单状态" prop="status">
              <el-select v-model="form.status" placeholder="订单状态">
                <el-option
                  v-for="dict in dict.type.svc_order_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 批量修改HPV接种状态对话框 -->
    <el-dialog :title="title" :visible.sync="updateall.open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="第一次接种" prop="one">
              <el-date-picker clearable
                              v-model="form.one"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择第一次接种时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第二次接种" prop="two">
              <el-date-picker clearable
                              v-model="form.two"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择第二次接种时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="第三次接种" prop="three">
              <el-date-picker clearable
                              v-model="form.three"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择第三次接种时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接种机构" prop="org">
              <el-select v-model="form.org" placeholder="请选择接种机构">
                <el-option
                  v-for="dict in dict.type.svc_jg_name"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>

          <el-col :span="12">
            <el-form-item label="订单状态" prop="status">
              <el-select v-model="form.status" placeholder="订单状态">
                <el-option
                  v-for="dict in dict.type.svc_order_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAllForm">确 定</el-button>
        <el-button @click="updateall.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的订单数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHpvschedules, getHpvorder, delHpvorder, addHpvorder, updateHpvorder, updateAllHpvorder } from "@/api/system/hpvschedules";
import { getToken } from "@/utils/auth";

export default {
  name: "Hpvorder",
  dicts: ['svc_xs_name', 'svc_qd_name', 'sys_user_sex', 'svc_user_marry', 'sys_pro_type', 'svc_jg_name', 'svc_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // HPV接种信息表格数据
      hpvorderList: [],
      // 报告时间范围
      daterangeOrdertime: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（订单导入）
        open: false,
        // 弹出层标题（订单导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的订单数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/hpvorder/importData"
      },
      updateall : {
        // 是否显示弹出层（批量修改）
        open: false
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ordertime: null,
        price: null,
        protype: null,
        cusname: null,
        cusidcard: null,
        phonenumber: null,
        sex: null,
        marry: null,
        status: null,
        org: null,
        one: null,
        two: null,
        three: null,
        sales: null,
        channel: null,
        report: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        protype: [
          { required: true, message: "项目类别不能为空", trigger: "change" }
        ],
        cusname: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
        cusidcard: [
          { required: true, message: "用户身份证号不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询HPV接种信息列表 */
    /*getList() {
      //console.log(dict)
      this.loading = true;
      listHpvorder(this.queryParams).then(response => {
        this.hpvorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },*/
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrdertime && '' != this.daterangeOrdertime) {
        this.queryParams.params["beginOrdertime"] = this.daterangeOrdertime[0];
        this.queryParams.params["endOrdertime"] = this.daterangeOrdertime[1];
      }

      listHpvschedules(this.queryParams).then(response => {
        this.hpvorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ordertime: null,
        price: null,
        protype: null,
        cusname: null,
        cusidcard: null,
        phonenumber: null,
        sex: "1",
        marry: "0",
        status: "1",
        org: null,
        one: null,
        two: null,
        three: null,
        sales: null,
        remark: null,
        channel: null,
        report: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrdertime = [];
      this.daterangeOne = [];
      this.daterangeTwo = [];
      this.daterangeThree = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加HPV接种信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHpvorder(id).then(response => {
        let data = response.data;
        if(data.channel!=null){
          data.channel = data.channel.toString();
        }
        if(data.sales!=null){
          data.sales = data.sales.toString();
        }
        if(data.protype!=null){
          data.protype = data.protype.toString();
        }
        if(data.org!=null){
          data.org = data.org.toString();
        }
        // data.channel = data.channel.toString();
        // data.sales = data.sales.toString();
        // data.protype = data.protype.toString();
        // data.org = data.org.toString();
        this.form = data;
        this.open = true;
        this.title = "修改HPV接种信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHpvorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHpvorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    submitAllForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.phonenumber = this.ids.join(",");
          updateAllHpvorder(this.form).then(response => {
            this.$modal.msgSuccess("批量修改成功");
            this.updateall.open = false;
            this.getList();
          });
        }
      });
    },

    /** 批量修改操作 **/
    handleUpdateAll(row) {
      const ids = row.id || this.ids;
      console.log(ids);
      this.reset();
      this.updateall.open = true;
      this.title = "批量修改HPV接种信息";
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除HPV接种信息编号为"' + ids + '"的数据项？').then(function() {
        return delHpvorder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.params = {};
      if (null != this.daterangeOrdertime && '' != this.daterangeOrdertime) {
        this.queryParams.params["beginOrdertime"] = this.daterangeOrdertime[0];
        this.queryParams.params["endOrdertime"] = this.daterangeOrdertime[1];
      }
      if (null != this.daterangeOne && '' != this.daterangeOne) {
        this.queryParams.params["beginOne"] = this.daterangeOne[0];
        this.queryParams.params["endOne"] = this.daterangeOne[1];
      }
      if (null != this.daterangeTwo && '' != this.daterangeTwo) {
        this.queryParams.params["beginTwo"] = this.daterangeTwo[0];
        this.queryParams.params["endTwo"] = this.daterangeTwo[1];
      }
      if (null != this.daterangeThree && '' != this.daterangeThree) {
        this.queryParams.params["beginThree"] = this.daterangeThree[0];
        this.queryParams.params["endThree"] = this.daterangeThree[1];
      }
      this.download('system/hpvschedules/export', {
        ...this.queryParams
      }, `客户排期信息表_${new Date().getFullYear()}-${new Date().getMonth()+1}-${new Date().getDate()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "订单导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/hpvorder/importTemplate', {
      }, `客户接种信息表_${new Date().getFullYear()}-${new Date().getMonth()+1}-${new Date().getDate()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
